var express=require('express')
var app=express();//接通前台后台的一个模块
app.use(express.static(__dirname));//建立一个临时文件夹
var users=[
{number:1,name:'男士运动鞋',price:799,brand:'李宁',inventory:999},
{number:2,name:'男士运动裤',price:500,brand:'森马',inventory:2000},
{number:3,name:'男士牛仔裤',price:1000,brand:'阿迪达斯',inventory:1992},
{number:4,name:'男士T恤',price:399,brand:'以纯',inventory:7656},
{number:5,name:'女士香水',price:5500,brand:'香奈儿',inventory:9889},
{number:6,name:'女士连衣裙',price:999,brand:'阿依莲',inventory:10000},
{number:7,name:'女士高跟鞋',price:2500,brand:'可情小美',inventory:8764},
{number:8,name:'男士手表',price:15000,brand:'劳力士',inventory:5555},
{number:9,name:'女士口红',price:8798,brand:'雅诗兰黛',inventory:1111},
{number:10,name:'女士长筒袜',price:199,brand:'穿我喜欢',inventory:100000},
{number:11,name:'24k纯金项链',price:200000,brand:'24k',inventory:100},
{number:12,name:'苹果10plus',price:12000,brand:'iPhone',inventory:6789},
{number:13,name:'不用上班卡',price:90000000,brand:'你们喂我袋盐',inventory:1},
{number:14,name:'不用上学卡',price:80000000,brand:'自己喂自己袋盐',inventory:2},
{number:15,name:'老司机开车卡',price:1,brand:'新15班都是老司机',inventory:20},
]
app.get('/list',function(req,res){//给网页发送数组
    res.send(users)
});
app.get('/',function(req,res){
  res.sendFile(__dirname+'/closer.html')//让网页跳转到closer.html
})
app.get('/ad',function(req,res){
    var n=req.query.name;
    var p=req.query.price;
    var b=req.query.brand;
    var i=req.query.inventory;
    var a={number:users.length+1,name:n,price:p,brand:b,inventory:i}//把数据打包成对象，传到数组里
    res.send('a')
    users.push(a)//把A添加到数组里
})
app.get('/modify',function(req,res){//回复modify的请求
    var num=req.query.number;//分解
    var n=req.query.name;
    var p=req.query.price;
    var b=req.query.brand;
    var invent=req.query.inventory;
    for(var i=0;i<users.length;i++){//循环原数组
        var s=users[i]
        if(s.number==num){
            s.name=n;
            s.price=p;
            s.brand=b;
            s.inventory=invent;
            res.send('a')//如果传来的数值与原数组的数值相等，就把对应的对象其他值改成传来的值
            return
        }
    }res.send('f')//否知报错
})
app.get('/dele',function(req,res){
   
     
    if(req.query.name!=undefined){
    var  s=req.query.name;
    for(var j=0;j<s.length;j++){

    for(var i=0;i<users.length;i++){
        var u=users[i]
        if(s[j]==u.number){
            users.splice(u,1);//数组里要删除的对应下表，只删除一个
           
          }
    }
     }
   for(var i=0;i<users.length;i++){
       users[i].number=i+1//重新排序
   } 
    res.send('s')//返回S删除成功
    
}else{
    res.send('f')//返回F删除失败
}
})
app.get('/search',function(req,res){//搜索
    var s=req.query.id;
   
     var arr=[];//建立一个空数组
    for(var i=0;i<users.length;i++){
       var  u=users[i]
        if(users[i].name.indexOf(s)>-1||users[i].brand.indexOf(s)>-1){//如果检测类型找到了
            arr.push(u)//就把他加入数组里
        }
    }
    res.send(arr)//把遍历完的数组放入空数组里
    
})
app.listen(8888)